package com.hyl.domain.activity.service;

import com.hyl.domain.activity.adapter.repository.IActivityRepository;
import com.hyl.domain.activity.model.entity.MarketProductEntity;
import com.hyl.domain.activity.model.entity.TrialBalanceEntity;
import com.hyl.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import com.hyl.domain.activity.model.valobj.TeamStatisticVO;
import com.hyl.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import com.hyl.types.design.framework.tree.StrategyHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hyl
 * @description
 * @create 2025-06-15 16:12
 */
@Service
public class IIndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService{
    @Resource
    private DefaultActivityStrategyFactory defaultActivityStrategyFactory;
    @Resource
    private IActivityRepository repository;
    @Override
    public TrialBalanceEntity indexGroupBuyMarket (MarketProductEntity marketProductEntity)throws Exception {

        StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> strategyhanddler = defaultActivityStrategyFactory.strategyHandler();
        TrialBalanceEntity trialBalanceEntity = strategyhanddler.apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
        return trialBalanceEntity;
    }


    @Override
    public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailList(Long activityId, String userId, Integer ownerCount, Integer randomCount) {
        List<UserGroupBuyOrderDetailEntity> unionAllList = new ArrayList<>();
        //查询个人数据
        if(0!=ownerCount){
            List<UserGroupBuyOrderDetailEntity> ownerList = repository.queryInProgressUserGroupBuyOrderDetailListByOwner(activityId, userId, ownerCount);
            if (null!= ownerList&&!ownerList.isEmpty()){
                unionAllList.addAll(ownerList);
            }
        }
        //查询其他非个人拼团
        if(0!=randomCount){
            List<UserGroupBuyOrderDetailEntity> randomList = repository.queryInProgressUserGroupBuyOrderDetailListByRandom(activityId, userId, randomCount);
            if (null!= randomList&&!randomList.isEmpty()){
                unionAllList.addAll(randomList);
            }
        }

        return unionAllList;
    }

    @Override
    public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
        return repository.queryTeamStatisticByActivityId(activityId);
    }
}


















